package cn.oms.oms.service.impl;

import cn.oms.oms.entity.Item;
import cn.oms.oms.exception.ServiceException;
import cn.oms.oms.mapper.ItemMapper;
import cn.oms.oms.service.IItemService;
import cn.oms.oms.vo.ItemVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;

    /**
     * 根据查询条件查询产品列表
     * @param itemVo
     * @return
     */
    @Override
    public PageInfo<Item> selectItemList(ItemVo itemVo) {
        QueryWrapper<Item> query = new QueryWrapper<>();

        if (StringUtil.isNotEmpty(itemVo.getItemName())){
            query.like("itemName",itemVo.getItemName())
                    .or()
                    .like("itemNameSN",itemVo.getItemName())
                    .or()
                    .like("ingredientName",itemVo.getItemName());
        }
        query.eq("deleteFlag",0);
        PageHelper.startPage(itemVo.getPageNum(),itemVo.getPageSize());
        List<Item> items = itemMapper.selectList(query);

        for (Item item:items){
            Item item1 = itemParamFormat(item);
            BeanUtils.copyProperties(item1,item);
        }

        return new PageInfo<>(items);
    }


    /**
     * 根据查询条件查询产品列表
     * @param itemVo
     * @return
     */
    @Override
    public Item selectItemDetail(ItemVo itemVo) {
        if (itemVo.getItemId()==null||"".equals(itemVo.getItemId())){
            throw new ServiceException("参数异常，产品ID为空");
        }
        QueryWrapper<Item> query = new QueryWrapper<>();
        query.eq("itemId",itemVo.getItemId());
        query.eq("deleteFlag",0);

        Item item = itemMapper.selectOne(query);
        if (item==null){
            throw new ServiceException("参数异常，未找到产品信息");
        }

        Item item1 = itemParamFormat(item);

        return item1;
    }

    /**
     * 新增产品
     * @param itemVo
     * @return
     */
    @Override
    public String addItem(ItemVo itemVo) {
        Item item = new Item();
//        item.setItemName(itemVo.getItemName());
//        item.setItemNameSN(itemVo.getItemNameSN());
//        item.setItemSpecificationDetail(itemVo.getItemSpecificationDetail());
//        item.setIngredientName(itemVo.getIngredientName());
//        item.getItemType(itemVo.getItemType());
        BeanUtils.copyProperties(itemVo,item);

        item.setCreateTime(new Date());
        item.setDeleteFlag(0);

        int num = itemMapper.insert(item);

        if (num!=1){
            throw new ServiceException("数据库新增异常！！！");
        }
        return "ok";
    }

    /**
     * 修改产品
     * @param itemVo
     * @return
     */
    @Override
    public String updateItem(ItemVo itemVo) {

        if(itemVo.getItemId()==null||"".equals(itemVo.getItemId())){
            throw new ServiceException("参数异常，产品ID为空");
        }
        QueryWrapper<Item> query = new QueryWrapper<>();
        query.eq("itemId",itemVo.getItemId());
        query.eq("deleteFlag",0);
        Item item = itemMapper.selectOne(query);
        if (item==null){
            throw new ServiceException("参数异常，未找到产品信息");
        }
        BeanUtils.copyProperties(itemVo,item);
        int num = itemMapper.updateById(item);

        if (num!=1){
            throw new ServiceException("数据库修改异常！！！");
        }
        return "ok";
    }

    /**
     * 删除产品
     * @param id
     * @return
     */
    @Override
    public String deleteItem(Integer id) {

        if (id==null||"".equals(id)){
            throw new ServiceException("参数异常，产品ID不能为空");
        }
        QueryWrapper<Item> query = new QueryWrapper<>();
        query.eq("itemId",id);
        query.eq("deleteFlag",0);
        Item item = itemMapper.selectOne(query);
        if (item==null){
            throw new ServiceException("参数异常，产品未找到");
        }
        item.setDeleteFlag(1);
        int num = itemMapper.updateById(item);

        if (num!=1){
            throw new ServiceException("数据库修改异常！！！");
        }
        return "ok";
    }


    /**
     * 参数格式化
     * @param item
     * @return
     */
    public Item itemParamFormat(Item item){
        Integer itemType = item.getItemType();
        if (itemType==0){
            item.setReturnItemTypeName("中药");
        }
        if (itemType==1){
            item.setReturnItemTypeName("西药");
        }
        if (itemType==2){
            item.setReturnItemTypeName("其他");
        }

        Integer prescriptionFlag = item.getPrescriptionFlag();
        if (prescriptionFlag==0){
            item.setReturnPrescriptionFlag("处方用药");
        }
        if (prescriptionFlag==1){
            item.setReturnPrescriptionFlag("非处方用药");
        }

        Date createTime = item.getCreateTime();
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createTime);
        if (StringUtil.isNotEmpty(date)){
            item.setReturnCreateTime(date);
        }
        return item;
    }
}
